function MrTable(t){
    var that = this;
    var $table = $(t);
    var $head = $table.getElement('tHead');
    var $body = $table.getElement('tBody');
    var columnSizes = null;
    
    var $container = new Element('div', {
        'class':'mrtablecontainer'
    }).inject($table, 'after');
    
    //create body and head containers
    var $bContainer = new Element('div', {
        'class':'mrbodycontainer'
    });
    
    var $hContainer = new Element('div', {
        'class':'mrheadcontainer'
    });
    
    //Create a new table to contain the header
    var $hTable = new Element('table');
    
    
    //Methods
    this.resize = function(){
        var h1 = $container.getStyle('height').toInt();
        var h2 = $hTable.getSize().y;
        
        $bContainer.setStyle('height', h1-h2);
    }
    
    this.getFullColumn = function(col){
        var column = $head.getElements('tr').getElements('th').map(function(item){
            return $(item[col]);
        });
        
        return column.concat(that.getColumn(col));
    };
    
    this.getColumn = function(col){
        return $body.getElements('tr').getElements('td').map(function(item){
            return $(item[col]);
        });
    };
    
    this.setFullColumnWidth = function(col, width){
        this.getFullColumn(col).each(function(c, i){
            c.setStyle('width', width);
        });
    };
    
    this.setColumnWidth = function(col, width){
        this.getColumn(col).each(function(c, i){
            c.setStyle('width', width);
        });
    };
    
    //Copy original table values into a new div
    $container.setStyles($table.getStyles('position', 
        'margin', 'top', 'left', 'right', 'bottom', 'background',
        'border-bottom', 'border-left', 'border-right', 'border-top'));
    
    $container.setStyles($table.getStyles('width', 'height'));
    
    $table.setStyle('width', 'auto');
    var tableSize = $table.getSize();
    $table.setStyle('width', tableSize.x);
    
    //Copy necessary values for header
//            $hTable.setStyles($table.getStyles( 'border-collapse', 'background', 
//                'padding-top', 'padding-left', 'padding-right'));//, 
//                'border-left', 'border-right', 'border-top'));
    
    $hTable.setStyle('width', tableSize.x);
    
    //Setting real cell width
    columnSizes = $head.getElements('th').getComputedStyle('width');
    columnSizes.each(function(c,i){
        this.setFullColumnWidth(i, c);
    }, this);
    
    //Adding the table into the container
    $bContainer.grab($table);
    $container.adopt($bContainer);
    
    //Grab the header
    $hTable.grab($head);
    $hContainer.adopt($hTable);
    $hContainer.inject($bContainer, 'before');
    
    //Handlers
    $bContainer.addEvent('scroll', function(){
        $hTable.setPosition({'y':this.scrollTop, 'x':0});
        $hContainer.scrollTo(this.scrollLeft, 0);
    });
    
    //Resize the table according to the new values
    this.resize();
    
    //Adjust hContainer to same width as bContainer
    $hContainer.setStyle('width', $bContainer.clientWidth);
}

window.addEvent('domready', function(){
    $$('table.mrtable').each(function(h){
        ttt = new MrTable(h);
    });
});
